/*
 * Copyright 2012 Daniel Kurka
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.googlecode.mgwt.expenses.client.activities.el;

import java.util.List;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.inject.Inject;
import com.googlecode.mgwt.expenses.client.ContextProvider;
import com.googlecode.mgwt.expenses.client.event.ExpenseEntryCreatedEvent;
import com.googlecode.mgwt.expenses.client.event.ExpenseEntrySelectedEvent;
import com.googlecode.mgwt.expenses.client.event.ExpenseEntryUpdatedEvent;
import com.googlecode.mgwt.expenses.client.rpc.ExpenseDataService;

public class ExpenseListActivity extends AbstractActivity implements ExpenseListView.Presenter {

	private final ExpenseListView view;
	private final ExpenseDataService expenseDataService;

	private List<ExpenseListDTO> currentModel;
	private EventBus eventBus;
	private boolean callPending;
	private boolean createCallRunning;
	private int selectedIndex;
	private final ContextProvider contextProvider;

	@Inject
	public ExpenseListActivity(ExpenseListView view, ExpenseDataService expenseDataService, ContextProvider contextProvider) {
		this.view = view;
		this.expenseDataService = expenseDataService;
		this.contextProvider = contextProvider;

	}

	@Override
	public void start(AcceptsOneWidget panel, EventBus eventBus) {
		this.eventBus = eventBus;

		this.selectedIndex = -1;

		ExpenseEntryCreatedEvent.register(eventBus, new ExpenseEntryCreatedEvent.Handler() {

			@Override
			public void onExpenseEntrySelected(ExpenseEntryCreatedEvent event) {
				loadList();

			}
		});

		ExpenseEntryUpdatedEvent.register(eventBus, new ExpenseEntryUpdatedEvent.Handler() {

			@Override
			public void onExpenseEntrySelected(ExpenseEntryUpdatedEvent event) {
				loadList();

			}
		});

		createCallRunning = false;

		view.setPresenter(this);
		view.setTitleText("Expenses");

		loadList();

		panel.setWidget(view);

	}

	private void loadList() {
		if (callPending)
			return;

		callPending = true;

		view.setStatusText("Loading...");

		expenseDataService.loadExpenseList(new AsyncCallback<List<ExpenseListDTO>>() {

			@Override
			public void onSuccess(List<ExpenseListDTO> result) {
				currentModel = result;
				view.display(result);
				view.setStatusText("");

				callPending = false;
				view.display(currentModel);
				updateIndex();

			}

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				callPending = false;

			}
		});

	}

	protected void updateIndex() {
		String currentExpsenseId = contextProvider.getCurrentExpsenseId();

		int counter = 0;
		for (ExpenseListDTO es : currentModel) {
			if (currentExpsenseId.equals(es.getId())) {
				selectedIndex = counter;
				view.selectItem(selectedIndex, true);
				break;
			}
			counter++;
		}

	}

	@Override
	public void onStop() {
		super.onStop();
		view.setPresenter(null);
	}

	@Override
	public void onItemSelected(int index) {

		if (selectedIndex != -1) {
			view.selectItem(selectedIndex, false);
		}

		if (index == -1) {
			return;
		}

		view.selectItem(index, true);
		selectedIndex = index;

		String id = currentModel.get(index).getId();
		ExpenseEntrySelectedEvent.fire(eventBus, id);

	}

	@Override
	public void onAddButtonSelected() {

		if (createCallRunning)
			return;

		createCallRunning = true;

		expenseDataService.createExpense(new AsyncCallback<String>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				createCallRunning = false;
			}

			@Override
			public void onSuccess(String result) {
				createCallRunning = false;
				ExpenseEntryCreatedEvent.fire(eventBus, result);

			}
		});

	}

	@Override
	public void onReloadButtonSelected() {
		loadList();

	}

}
